// ==UserScript==
// @name         YBFetchAsc
// @namespace    https://bbs.tampermonkey.net.cn/
// @version      0.1.0
// @description  try to take over the world!
// @author       You ScriptCat
// ScriptCat
// @grant GM_xmlhttpRequest
// @match        https://wm.xuanqingkj.cn/jrNJyBfHxQ.php/dashboard*
// ==/UserScript==

(function () {
    'use strict';
    let url_task_log = 'http://yunbao.test.com/index.php/fetcher/takeTask';
    let url_save = 'http://yunbao.test.com/index.php/fetcher/save'

    run();

    function getURLParam(name) {
        const queryString = window.location.search;
        const urlParams = new URLSearchParams(queryString);
        rt = '';
        if (name) {
            rt = urlParams.get(name);
        }
        return rt;
    }


    function run() {
        fetch_url_id = getURLParam('fetch_url_id')
        url = url_task_log + '?t=' + new Date().getTime() + '&fetch_url_id=' + fetch_url_id
        let req = {
            url: url,
            method: "GET",
            onload: function (xhr) {
                console.log(xhr.responseText)
                let urlJSON = JSON.parse(xhr.responseText)
                if (urlJSON) {
                    for (let key in urlJSON) {
                        fetchData(urlJSON[key], key)
                    }
                } else {
                    setTimeout(run, 1000);
                }
            }
        }
        GM_xmlhttpRequest(req);
    }

    function fetchData(url, key) {
        fetch(url, {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json',
                    'X-Requested-With': 'XMLHttpRequest',
                },
            }
        ).then(response => {
            if (!response.ok) {
                console.log('!response.ok' + url)
                return false
            }
            return response.json();
        }).then(
            data => {
                if (data.rows && data.rows.length > 0) {
                    console.log("data.rows.length:" + data.rows.length)
                } else {
                    console.log("data.rows.length:")
                }
                let fetch_act = getURLParam('fetch_act')
                let url = url_save + '?fetch_act=' + fetch_act + '&fetch_log_id=' + key + '&remote_total=' + data.total;

                let req = {
                    url: url,
                    method: "POST",
                    data: 'data=' + encodeURIComponent(JSON.stringify(data.rows)),
                    headers: {"Content-type": "application/x-www-form-urlencoded"},
                    onload: function (xhr) {
                        console.log(xhr.responseText.substring(0, 200));
                        let fetch_not_loop = getURLParam('not_loop')
                        if (!fetch_not_loop) {
                            run()
                        }
                    }
                }
                GM_xmlhttpRequest(req);
            }
        )
    }
})();